package com.cmc.printApplet.receiver;

import com.alibaba.fastjson.JSON;
import com.cmc.printApplet.CirculateOrder.DrawInbound;
import com.cmc.printApplet.service.PrintdInboundServices;
import com.cmc.printApplet.util.PageFormatSub;
import com.cmc.printApplet.vo.PruchaseInbound;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.awt.print.Book;
import java.awt.print.Paper;

/**
 * 采购入库单接收方法
 * 注意queueName 一定要和T_SYSTEM_ORG表orgId保持一致
 */
@Component
@RabbitListener(queues = "${spring.profiles.inbound}")
public class InboundMqReceiver {
    private final Logger logger = LoggerFactory.getLogger(InboundMqReceiver.class);

    @Value("${spring.profiles.orgId}")
    private String orgIds;

    @RabbitHandler
    public void process(String msg){
        PruchaseInbound inbound = JSON.parseObject(msg,PruchaseInbound.class);
        PrintdInboundServices InboundPrint = new PrintdInboundServices();
        logger.info("接收到采购入库单打印信息，开始判断仓库Id是否正确"+inbound.getOrgId());
        logger.info("打印人Id:"+inbound.getPeopleId()+";打印人姓名:"+inbound.getPeopleName()+";打印时间:"+inbound.getPrintTime()+";打印仓库名称:"+inbound.getOrgName());
        for (String orgId:orgIds.split(";")){
            if (orgId.equals(inbound.getOrgId())){
                logger.info("仓库匹配成功仓库Id["+orgId+"]，开始生成打印对象");
                logger.info("打印信息为："+msg);
                DrawInbound pageFirst = new DrawInbound(inbound);
                logger.info("设置纸张");
                Paper p = new Paper();
                p.setSize(pageFirst.WIDTH_NUM, pageFirst.HEIGTH_NUM);// 纸张大小
                p.setImageableArea(0, 0, pageFirst.WIDTH_NUM, pageFirst.HEIGTH_NUM);

                PageFormatSub pf = new PageFormatSub();
                pf.setOrientation(PageFormatSub.PORTRAIT);
                pf.setPaper(p);

                Book book = new Book();
                book.append(InboundPrint, pf);
                logger.info("初始化打印");
                InboundPrint.dealPrint(pageFirst, book,inbound);
                System.out.println("Receiver  : " + msg);
            }
        }



    }
}
